package com.be.beadmin.basicData.controller;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.fastjson.JSONArray;
import com.be.beadmin.basicData.dto.input.OrgParentFilter;
import com.be.beadmin.basicData.dto.output.OrganizationExcelEx;
import com.be.beadmin.basicData.entity.Organization;
import com.be.beadmin.basicData.dto.output.OrganizationEx;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.basicData.service.OrganizationService;
import com.be.beadmin.basicData.dto.input.OrganizationQueryCriteria;
import com.be.beadmin.basicData.dto.input.OrganizationDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;

/**
* @author chenwei
* @date 2023-04-18
**/
@RestController
public class OrganizationController {

    @Autowired
    private  OrganizationService organizationService;


    @PostMapping(value = "/basicData/getOrganizationByPage")
    public ResultModel getOrganizationByPage(@RequestBody OrgParentFilter pageEntity)  {
        return ResultModel.success(organizationService.getOrganizationByPage(pageEntity)) ;
    }

    @ApiOperation("组织树")
    @PostMapping("/basicData/getOrgTree")
    public ResultModel getOrgTree(){
        JSONArray array = organizationService.getOrgTree();
        return ResultModel.success(array);
    }


    @PostMapping("/basicData/addOrganization")
    public ResultModel addOrganization(@RequestBody OrganizationDto organizationDto){
        organizationService.addOrganization(organizationDto);
        return ResultModel.success(ResultModel.ADD);
    }



    @PostMapping("/basicData/delOrganization")
    public ResultModel delOrganization(@RequestBody OrganizationDto organizationDto){
        organizationService.delOrganization(organizationDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/basicData/updateOrganization")
    public ResultModel updateOrganization(@RequestBody OrganizationDto organizationDto){
        organizationService.updateOrganization(organizationDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/basicData/exportOrganization")
    public void exportOrganization(HttpServletResponse response ,@RequestBody OrgParentFilter pageEntity) throws IOException {
        Page<OrganizationEx> organizationPage = organizationService.getOrganizationByPage(pageEntity);
        List<OrganizationEx> organizationExes = organizationPage.getContent();
        List<OrganizationExcelEx> organizationExcelExes=organizationExes.stream().map(item->{
            OrganizationExcelEx excel=new OrganizationExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("Organization接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), OrganizationExcelEx.class).sheet("sheet1").doWrite(organizationExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), OrganizationExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(organizationExcelExes);
    }
}